package com.vdg.fps.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;

import com.vdg.R;
import com.vdg.fps.data.FaceBookAccount;
import com.vdg.fps.data.GroupsData;
import com.vdg.fps.data.Member;
import com.vdg.fps.data.MemberPostingData;
import com.vdg.fps.ui.view.TextViewPlus;
import com.vdg.fps.ulti.Utilities;

public class ListMembersPostingAdapter extends ArrayAdapter<MemberPostingData> {
	private List<MemberPostingData> mListMembers;
	private ArrayList<MemberPostingData> mOrgListMembers;
	private boolean mIsSelected;

	public static enum POST_TYPE {
		PRIVATE_MESSAGE, POST, PHOTO_POST
	}

	private POST_TYPE mPostType;

	public ListMembersPostingAdapter(Context context, int textViewResourceId,
			List<MemberPostingData> objects, POST_TYPE postype) {
		super(context, textViewResourceId, objects);
		mOrgListMembers = new ArrayList<MemberPostingData>();
		mListMembers = objects;
		mOrgListMembers.addAll(mListMembers);
		mPostType = postype;
	}

	public void setIsSelected(boolean isSelected) {
		mIsSelected = isSelected;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final MemberPostingData member = mListMembers.get(position);
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(getContext()).inflate(
					R.layout.post_message_row, parent, false);
			holder.mTxvName = (TextViewPlus) convertView
					.findViewById(R.id.txv_name);
			holder.mTxvID = (TextViewPlus) convertView
					.findViewById(R.id.txv_id);
			holder.mPostingID = (TextViewPlus) convertView
					.findViewById(R.id.txv_post_id);
			holder.mStatus = (TextViewPlus) convertView
					.findViewById(R.id.txv_status);
			holder.mDevPostID = (LinearLayout) convertView
					.findViewById(R.id.dev_post_id);
			holder.mDevStatus = (LinearLayout) convertView
					.findViewById(R.id.dev_status);
			holder.mChbSelected = (CheckBox) convertView
					.findViewById(R.id.chb_select_all);
			convertView.setTag(holder);
			holder.mDevSelected = (LinearLayout) convertView
					.findViewById(R.id.dev_selected);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (member != null) {
			String name = member.getName();
			String id = member.getID();
			String postID = member.getPostID();
			if (postID == null || postID.isEmpty()) {
				postID = "";
			}
			String status = member.getStatus();
			if (status == null || status.isEmpty()) {
				status = "";
			}
			holder.mTxvName.setText(name);
			holder.mTxvID.setText(id);
			if (holder.mPostingID != null) {
				holder.mPostingID.setText(postID);
			}
			holder.mStatus.setText(status);
			if (mIsSelected) {
				holder.mDevSelected.setVisibility(View.VISIBLE);
				holder.mChbSelected.setVisibility(View.VISIBLE);
				holder.mChbSelected
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {

							@Override
							public void onCheckedChanged(
									CompoundButton buttonView, boolean isChecked) {
								// TODO Auto-generated method stub
								member.setIsSelected(isChecked);
							}
						});
				if (holder.mPostingID != null) {
					holder.mPostingID.setVisibility(View.GONE);
				}
				holder.mStatus.setVisibility(View.GONE);
				if (holder.mDevPostID != null) {
					holder.mDevPostID.setVisibility(View.GONE);
				}
				holder.mDevStatus.setVisibility(View.GONE);
				if (member.isIsSelected()) {
					holder.mChbSelected.setChecked(true);
				} else {
					holder.mChbSelected.setChecked(false);
				}
			} else {
				holder.mDevSelected.setVisibility(View.GONE);
				holder.mChbSelected.setVisibility(View.GONE);
				if (holder.mPostingID != null) {
					holder.mPostingID.setVisibility(View.VISIBLE);
				}
				holder.mStatus.setVisibility(View.VISIBLE);
				if (holder.mDevPostID != null) {
					holder.mDevPostID.setVisibility(View.VISIBLE);
				}

				if (mPostType == POST_TYPE.PRIVATE_MESSAGE) {
					holder.mDevPostID.setVisibility(View.GONE);
					holder.mPostingID.setVisibility(View.GONE);
				}

				holder.mDevStatus.setVisibility(View.VISIBLE);
			}

		}
		return convertView;
	}

	public static class ViewHolder {
		private TextViewPlus mTxvName;
		private TextViewPlus mTxvID;
		private TextViewPlus mPostingID;
		private TextViewPlus mStatus;
		private LinearLayout mDevPostID;
		private LinearLayout mDevStatus;
		private CheckBox mChbSelected;
		private LinearLayout mDevSelected;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mListMembers.size();
	}

	public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		mListMembers.clear();
		if (charText.length() == 0) {
			mListMembers.addAll(mOrgListMembers);
		} else {
			for (MemberPostingData wp : mOrgListMembers) {
				if (wp.getName().toLowerCase(Locale.getDefault())
						.contains(charText)) {
					mListMembers.add(wp);
				}
			}
		}
		notifyDataSetChanged();
	}

	public void setSelectAll(boolean value) {
		for (MemberPostingData member : mListMembers) {
			member.setIsSelected(value);
		}
	}

	public MemberPostingData getItem(int position) {
		if (mListMembers == null || mListMembers.size() == 0) {
			return null;
		}
		return mListMembers.get(position);
	}

	public List<MemberPostingData> getDisplayData() {
		return mListMembers;
	}

	public List<MemberPostingData> getSelectedData() {
		ArrayList<MemberPostingData> listResult = new ArrayList<MemberPostingData>();
		for (MemberPostingData data : mListMembers) {
			if (data.isIsSelected()) {
				listResult.add(data);
			}
		}
		Collections.sort(listResult);
		return listResult;
	}
}
